package com.example.demo.service.impl;

import com.example.demo.entity.Safetyinspectionmonitoringsystem;
import com.example.demo.entity.Safetyinspectionspatialdata;
import com.example.demo.handle.MonitoringSystemListParseHandler;
import com.example.demo.handle.SafetyMonitoringListParseHandler;
import com.example.demo.mapper.SafetyinspectionspatialdataMapper;
import com.example.demo.service.SafetyinspectionspatialdataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-15
 */
@Service
public class SafetyinspectionspatialdataServiceImpl extends ServiceImpl<SafetyinspectionspatialdataMapper, Safetyinspectionspatialdata> implements SafetyinspectionspatialdataService {

    @Autowired
    private SafetyinspectionspatialdataMapper safetyinspectionspatialdataMapper;

    @Override
    public Integer insertCoalMineAttributesData(String safetyMonitoringXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            SafetyMonitoringListParseHandler safetyMonitoringListParseHandler = new SafetyMonitoringListParseHandler();
            try {
                //读取井口信息文件
                parser.parse(safetyMonitoringXml, safetyMonitoringListParseHandler);

            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + safetyMonitoringListParseHandler.getSafetyinspectionspatialdataArrayList().size() + "个信息");
            // 根据minecode去查找数据库
            for (Safetyinspectionspatialdata safetyinspectionspatialdata : safetyMonitoringListParseHandler.getSafetyinspectionspatialdataArrayList()) {
                System.out.println(safetyinspectionspatialdata.getCsMineCode());
                System.out.println(safetyinspectionspatialdata.getCsDataTime());
                System.out.println(safetyinspectionspatialdata.getSsGisnodeCode());
                System.out.println(safetyinspectionspatialdata.getCsYCoor());
                System.out.println(safetyinspectionspatialdata.getCsXCoor());
                System.out.println(safetyinspectionspatialdata.getCsZCoor());
                System.out.println("----------------------------安全监控信息finish---------------------------------");

                safetyinspectionspatialdataMapper.insertSafetyMonitoringData(safetyinspectionspatialdata.getCsMineCode(),safetyinspectionspatialdata.getCsDataTime(),safetyinspectionspatialdata.getSsGisnodeCode(),safetyinspectionspatialdata.getCsYCoor(),safetyinspectionspatialdata.getCsXCoor(),safetyinspectionspatialdata.getCsZCoor());
            }


        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
